//===================================================================================
// $$
//
//
// Code By: Code Milker V2
// Date On: 2013//
//===================================================================================


namespace Forest.Lion.Data.SqlServer.Repositories {
    using System;

    using System.Collections.Generic;

    using System.Data;

    using System.Linq;
    using Forest.Lion.Model;

    public class CandidateRepository : BaseRepository, ICandidateRepository {
        public CandidateRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Candidate candidate) {
            this.GetDbSet<Candidate>().Add(candidate);
            this.UnitOfWork.SaveChanges();
        }

        public Candidate GetCandidate(Guid candidateId) {
            return this.GetDbSet<Candidate>()
                .Where(c => c.Id == candidateId)
                .Single();
        }

        public void Update(Candidate updatedCandidate) {
            this.GetDbSet<Candidate>().Attach(updatedCandidate);
            this.SetEntityState(updatedCandidate, updatedCandidate.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid candidateId) {
            Candidate candidateToDelete = this.GetCandidate(candidateId);
            this.GetDbSet<Candidate>().Remove(candidateToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<Candidate> Find() {
            return this.GetDbSet<Candidate>().Take(100)
                .ToList();
        }

        public IQueryable<Candidate> GetAll()
        {
            return this.GetDbSet<Candidate>();
        }
    }
}
